﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using BaseUi.ViewModel;

namespace BaseUi
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow3 : Window
    {
        public static MainViewModel viewModel = MainWindow.viewModel;
        public MainWindow3()
        {
            if (!viewModel.InitAgo())
                this.Close();

            InitializeComponent();

            viewModel.Init(this);
            this.DataContext = viewModel;
        }

        private void load(object sender, RoutedEventArgs e)
        {
            MenuTest_MouseLeftButtonUp(selectBorder, null);
        }

        #region 菜单切换
        private void MenuTest_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "生产";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }

        private void MenuEdit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "调试";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }

        private void MenuData_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "查询";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }

        private void MenuSet_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "设置";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }
        #endregion

        #region 样式、效果

        //鼠标移动颜色
        SolidColorBrush leaveColor = new SolidColorBrush(Color.FromRgb(58, 144, 193));
        SolidColorBrush moveColor = new SolidColorBrush(Color.FromRgb(44, 177, 255));
        SolidColorBrush selectColor = new SolidColorBrush(Color.FromRgb(100, 185, 255));

        //菜单栏颜色切换
        private void ChangeMenuColor(Border selectBorder)
        {
            foreach (var item in StackPanelMenu.Children)
            {
                if (item is Border border)
                {
                    if (border == selectBorder)
                        border.Background = selectColor;
                    else
                        border.Background = leaveColor;
                }
            }
        }

        public bool MenuSelect(Border border)
        {
            return border.Background == selectColor;
        }

        //窗口移动事件
        private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        DateTime dts = DateTime.Now;
        //标题栏双击事件
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed)
                return;

            DateTime dtd = DateTime.Now;
            if ((dtd - dts).TotalMilliseconds <= 300)
                this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;

            dts = dtd;
        }

        private void BorderMin_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void BorderMax_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
                winMax.Visibility = Visibility.Visible;
                winMin.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
                winMax.Visibility = Visibility.Collapsed;
                winMin.Visibility = Visibility.Visible;
            }
        }

        private void BorderMenu_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            contextMenu.PlacementTarget = sender as Border;
            contextMenu.Placement = PlacementMode.Bottom;
            contextMenu.HorizontalOffset = 0;
            //显示菜单
            contextMenu.IsOpen = true;
        }

        private void BorderClose_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                border.Background = Brushes.Tomato;
        }

        private void BorderClose_MouseLeave(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                border.Background = leaveColor;
        }
        private void BorderClose_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBoxResult result = HandyControl.Controls.MessageBox.Show("确定退出？", "退出提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.OK)
            {
                this.Close();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void Menu_Leave(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                if (border.Background is SolidColorBrush solidColorBrush)
                    if (solidColorBrush.Color.R != selectColor.Color.R)
                        border.Background = leaveColor;
        }

        private void Menu_Enter(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                if (border.Background is SolidColorBrush solidColorBrush)
                    if (solidColorBrush.Color.R != selectColor.Color.R)
                        border.Background = moveColor;
        }

        private void tabControl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            //取消tabControl快捷键切换
            if (e.Key == Key.LeftCtrl || e.Key == Key.LeftCtrl || e.Key == Key.Tab)
                e.Handled = true;
        }
        #endregion
    }
}
